package geektime.spring.springbucks.mapper;

import geektime.spring.springbucks.model.Coffee;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.session.RowBounds;

import java.util.List;

@Mapper
public interface CoffeeMapper {
    @Insert("insert into t_coffee (name, price, create_time, update_time) values (#{name}, #{price}, now(), now())")
    @Options(useGeneratedKeys = true)
    int save(Coffee coffee);

    @Select("select * from t_coffee where id = #{id}")
    @Results({
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "create_time", property = "createTime")
    })

    Coffee findById(@Param("id") Long id);

    List<Coffee> findListById(@Param("ids") List<Long> ids);

    @Select("select * from t_coffee order by id")
    List<Coffee> findAllWithRowBounds(RowBounds rowBounds);

    @Select("select * from t_coffee order by id")
    List<Coffee> findAllWithParam(@Param("pageNum") int pageNum,
                                  @Param("pageSize") int pageSize);

    @Update({
            "update T_COFFEE",
            "set NAME = #{name,jdbcType=VARCHAR},",
            "PRICE = #{price,jdbcType=BIGINT,typeHandler=geektime.spring.springbucks.handler.MoneyTypeHandler},",
            "CREATE_TIME = #{createTime,jdbcType=TIMESTAMP},",
            "UPDATE_TIME = #{updateTime,jdbcType=TIMESTAMP}",
            "where ID = #{id,jdbcType=BIGINT}"
    })
    int updateByPrimaryKey(Coffee record);

    @Delete({
            "delete from T_COFFEE",
            "where ID = #{id,jdbcType=BIGINT}"
    })
    int deleteByPrimaryKey(Long id);

}